package com.zut.controller;
import com.zut.pojo.*;
import com.zut.pojo.vo.ProductVo;
import com.zut.service.ProductService;
import com.zut.service.RepayService;
import com.zut.service.UserService;
import com.zut.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@CrossOrigin
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;
    @Autowired
    private RepayService repayService;

    // 项目类型下拉框
    @GetMapping("/getType")
    public Result getAllProductType(String all) {
        List<Type> typeList = productService.selectAllProductType(all);
        if (typeList.size() != 0) {
            return Result.success(typeList);
        } else {
            return Result.error("无");
        }
    }

    // 发布项目
    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody Product product, HttpServletRequest req){
        String jwt = req.getHeader("Authorization");
        // 解析令牌
        String phone = (String) JwtUtils.parseJWT(jwt).get("phone");
        User user = userService.telTest(phone);
        if (null!=user){
            // 开始时间
            Calendar calendar1 = Calendar.getInstance();
            product.setStartTime(calendar1.getTime());
            product.setUserId(user.getId());
            calendar1.add(Calendar.DATE, product.getDays());
            // 结束时间
            product.setEndTime(calendar1.getTime());
            int i=productService.addProduct(product);
            Product product1 = productService.selectIdByProduct(product);
            if(i!=0){
                return Result.success(product1.getId());
            }
            return Result.error("项目发布失败了！！！");
        }
        return Result.error("还没登录呢！笨蛋");
    }

    // 查询所有项目
    @GetMapping("/getAllProjects")
    public Result selectAllProjects(){
        List<Product> list= productService.selectAllProjects();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy年MM月dd日 :hh:mm:ss");
        if(list.size()!=0){
            for (Product product : list) {
                // 根据typeId跟UserId   分别查出来type和user
                Type type = productService.selectTypeById(product.getTypeId());
                User user = userService.selectUserById(product.getUserId());
                Repay repay = repayService.selectRepayByProductId(product.getId());
                product.setUserName(user.getUsername());
                product.setTypeName(type.getName());
                // 项目进度，计算一下，封装一下
                BigDecimal JinDu = BigDecimal.valueOf((repay.getPaymoney()/product.getMoney())*100);
                product.setJindu(JinDu.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue()+"");
                // 开始结束时间，用于渲染前端页面
                product.setTime1(dateFormat.format(product.getStartTime()));
                product.setTime2(dateFormat.format(product.getEndTime()));
            }
            return Result.success(list);
        }else {
            return Result.error("还没有发布项目哦！！！");
        }
    }

    // 项目详情
    @GetMapping("/getProductById")
    public Result getProductById(@RequestParam("id") String id){
        Product product= productService.selectProductById(Integer.parseInt(id));
        User user = userService.selectUserById(product.getUserId());
        Type type = productService.selectTypeById(product.getTypeId());
        ArrayList<Object> list = new ArrayList<>();
        list.add(product);
        list.add(user);
        list.add(type);
        return Result.success(list);
    }
}
